// pages/checkOrder/checkOrder.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    movieArrangeInfo: '',
    selectSeatList: '',
    sumPrice: '',
    realPrice: '',
    movieStill: '',
    dateInfo: '',
    selectCinemaInfo: '',
    discountPrice: '',
    couponInfo: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this
    that.setData({
      movieArrangeInfo: wx.getStorageSync('selectMovieArrangeInfo'),
      selectSeatList: wx.getStorageSync('selectSeatList'),
      sumPrice: options.sumPrice,
      dateInfo: options.dateInfo,
      movieStill: wx.getStorageSync('movieStill'),
      sumTicket: wx.getStorageSync('selectSeatList').length,
      selectCinemaInfo: wx.getStorageSync('selectCinemaInfo'),
      realPrice: options.sumPrice,
    })
  },

  //跳转选择优惠券
  toUseCoupon() {
    var that = this
    wx.navigateTo({
      url: '../useCoupon/useCoupon?sumPrice=' + that.data.sumPrice,
    })
  },

  //提交订单
  check() {
    var that = this
    var tempId = that.data.selectSeatList[0].id
    var tempSeat = that.data.selectSeatList[0].seatX + '排' + that.data.selectSeatList[0].seatY + '座'
    for (var j = 1; j < that.data.selectSeatList.length; j++) {
      tempSeat = tempSeat + ',' + that.data.selectSeatList[j].seatX + '排' + that.data.selectSeatList[j].seatY + '座',
        tempId = tempId + ',' + that.data.selectSeatList[j].id
    }
    var orders = {
      movieArrangeId: that.data.movieArrangeInfo.id,
      seatId: tempId,
      username: wx.getStorageSync('username'),
      orderPay: that.data.realPrice,
      cinemaId: that.data.selectCinemaInfo.id,
      seatSelect: tempSeat
    }
    wx.request({
      url: 'http://localhost:8081/api/order/wxCheckOrders',
      data: {
        orders: orders,
        useCouponId: that.data.couponInfo.id
      },
      header: {
        'token': wx.getStorageSync('token')
      },
      method: "POST",
      success: function (res) {
        if (res.data.code == 10008 && res.data.result == "购票成功") {
          wx.showToast({
              title: '购票成功',
            }),
            setTimeout(function () {
              wx.redirectTo({
                url: '../orders/orders?toDetail=' + 1,
              })
            }, 1000)
        }else{
          wx.showToast({
            title: '余额不足',
            icon: 'error'
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this
    that.data.realPrice = that.data.sumPrice
    var temDiscountPrice
    if (that.data.couponInfo != '') {
      if (that.data.couponInfo.couponType == '满减券') {
        temDiscountPrice = that.data.couponInfo.couponDiscount
        that.data.realPrice = that.data.sumPrice - that.data.couponInfo.couponDiscount
      } else if (that.data.couponInfo.couponType == '折扣券') {
        that.data.realPrice = that.data.sumPrice * that.data.couponInfo.couponDiscount
        temDiscountPrice = that.data.sumPrice - that.data.realPrice
      } else {
        that.data.realPrice = 0
        temDiscountPrice = that.data.sumPrice
      }
      that.setData({
        realPrice: that.data.realPrice,
        discountPrice: temDiscountPrice
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})